package com.bprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bprogram.mapper.PartionMapper;
import com.bprogram.pojo.Partion;
import com.bprogram.pojo.User;
import com.bprogram.service.PartionService;

import com.bprogram.utils.IdGenerator;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 分区表 服务实现类
 * </p>
 *
 * @author cai
 * @since 2023-11-21
 */
@Service
public class PartionServiceImpl extends ServiceImpl<PartionMapper, Partion> implements PartionService {

    @Resource
    private PartionMapper partionMapper;

    @Override
    public boolean saveOrUpdate(Partion partion) {
        Partion partion1 = new Partion();
        if (StringUtils.isEmpty(partion.getPartionId())) {
            long partionId = new IdGenerator().snowflakeId();
            partion.setPartionId(partionId);
            return this.save(partion);
        }
        UpdateWrapper<Partion> wrapper = new UpdateWrapper<>();

        if (!StringUtils.isEmpty(partion.getPartionName())) {
            partion1.setPartionName(partion.getPartionName());
        }
        if (!StringUtils.isEmpty(partion.getPartionType())) {
            partion1.setPartionType(partion.getPartionType());
        }
        wrapper.eq("partionId",partion.getPartionId());
        boolean b = this.update(partion1, wrapper);
        return b;
    }
    @Override
    public  List<Partion> partionList(String currentPage, String pageSize, String type) {
        IPage<Partion> page = new Page<>();
        long ps = Long.parseLong(pageSize);
        long cp = Long.parseLong(currentPage);
        page.setSize(ps);
        page.setCurrent(cp);
        QueryWrapper<Partion> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(type)) {
            wrapper.eq("partionType", type);
        }
        IPage<Partion> partionPage = partionMapper.selectPage(page, wrapper);
        List<Partion> partionList = partionPage.getRecords();
        return partionList;
    }
    @Override
    public boolean partionDelete(long id) {
        int result = partionMapper.deleteById(id);
        if (result > 0) {
            return true;
        }
        return false;
    }
}
